package com.eqdd.yiqidian.ui.discover.college;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.LectureListBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration;
import com.eqdd.yiqidian.box.span.MovementMethod.ClickMovementMethod;
import com.eqdd.yiqidian.box.span.TagSpan;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

/**
 * @author吕志豪 .
 * @date 18-3-14  上午9:37.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class TrainListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private int pageNum;
    private SlimAdapterEx<LectureListBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "选择一个课程进行评价");

    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new DividerLineItemDecoration(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_44, new SlimInjector<LectureListBean>() {
            @Override
            public void onInject(@Nullable LectureListBean data, @Nullable IViewInjector injector) {

                injector.text(R.id.tv_upper_content, data.getCourseTheme())
                        .with(R.id.tv_down_content, view -> {
                            TextView textView = (TextView) view;
                            textView.setText("");
                            String[] split = data.getCourseType().split(",");
                            for (String s : split) {
                                SpannableString spanString = new SpannableString(s);
                                TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);
//                                tagSpan.setTextClick(text -> {
//                                    Bundle bundle = new Bundle();
//                                    bundle.putInt(Config.POS, 1);
//                                    bundle.putString(Config.FIELD, text.toString());
//                                    startActivity(LearnSearchActivity.class, bundle);
//                                });
                                spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                textView.append(spanString);
                            }
                            textView.setMovementMethod(new ClickMovementMethod());
                        });
            }
        }).attachTo(dataBinding.recyclerView);
        ModelRx2DataSource<LectureListBean> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                pageNum = page == 1 ? 0 : pageNum;
                return OkGo.<ZhangPageResultBean<List<LectureListBean>>>post(HttpConfig.BASE_URL + HttpConfig.COLLEGE_LECTURE_LIST)
                        .params("page", pageNum)
                        .params("userGuid", AppConstent.user.getGuid())
                        .converter(new JsonConverter<ZhangPageResultBean<List<LectureListBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        }).adapt(new FlowableBody<>())
                        .flatMap(listResultBean -> {
                            pageNum = listResultBean.getNextpage();
                            if (listResultBean.getStatus() == 200) {
                                return Flowable.just(listResultBean.getItems() == null ? new ArrayList<LectureListBean>() : listResultBean.getItems());
                            } else {
                                ToastUtil.showShort(listResultBean.getMsg());
                                return Flowable.just(new ArrayList<>());
                            }
                        });
            }
        });
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putInt(Config.ID,slimAdapterEx.getDataItem(position).getId());
                    startActivity(EvaluateActivity.class,bundle);
                });
        slimAdapterEx.updateData(new ArrayList<>());
        MVCCoolHelper<List<LectureListBean>> mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.refresh();
    }

    @Override
    public void setView() {

    }
}
